package com.swj.modules.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swj.common.core.constant.UserConstants;
import com.swj.common.core.model.SysDictType;
import com.swj.common.core.model.web.RestResult;
import com.swj.common.core.utils.ResponseUtil;
import com.swj.common.core.utils.poi.ExcelUtil;
import com.swj.common.log.annotation.Log;
import com.swj.common.log.enums.BusinessType;
import com.swj.common.security.annotation.RequiresPermissions;
import com.swj.modules.system.model.SysDictTypeQueryVO;
import com.swj.modules.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/dict/type")
public class SysDictTypeController  {
    @Autowired
    private ISysDictTypeService dictTypeService;

    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    public RestResult<List<SysDictType>> list(SysDictTypeQueryVO dictType) {
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return ResponseUtil.successWithList(list);
    }

    /**
     * 分页查询字典类型列表
     */
    @RequiresPermissions("system:dict:list")
    @GetMapping("/page")
    public RestResult<Page<SysDictType>> page(SysDictTypeQueryVO dictType) {
        Page<SysDictType> page = dictTypeService.selectDictTypePage(dictType);
        return ResponseUtil.successWithPageList(page);
    }

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictTypeQueryVO dictType) {
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<>(SysDictType.class);
        util.exportExcel(response, list, "字典类型");
    }

    /**
     * 查询字典类型详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictId}")
    public RestResult<SysDictType> getInfo(@PathVariable String dictId) {
        return ResponseUtil.successWithObjAndMsg(dictTypeService.selectDictTypeById(dictId),"查询成功");
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public RestResult<?> add(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return  ResponseUtil.failWithError("修改失败","修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        if(dictTypeService.insertDictType(dict)){
            return ResponseUtil.success("添加成功");
        }
        return ResponseUtil.fail("修改失败");
    }

    /**
     * 修改字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public RestResult<?> edit(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return ResponseUtil.failWithError("修改失败","修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        //dict.setUpdateBy(SecurityUtils.getUsername());
        if(dictTypeService.updateDictType(dict)){
            return ResponseUtil.success("修改成功");
        }
        return ResponseUtil.fail("修改成功");
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public RestResult<String> remove(@PathVariable String[] dictIds) {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return ResponseUtil.success("删除成功");
    }

    /**
     * 刷新字典缓存
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public RestResult<?> refreshCache() {
        dictTypeService.resetDictCache();
        return ResponseUtil.success("刷新缓存成功");
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public RestResult<List<SysDictType>> optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return ResponseUtil.successWithList(dictTypes);
    }
}
